package com.orchid.community.models.controller;

import com.orchid.community.common.utils.CodeEnum;
import com.orchid.community.common.utils.MsgResponse;
import com.orchid.community.common.utils.ResultUtils;
import com.orchid.community.common.utils.ValidateNumber;
import com.orchid.community.models.DTO.AccountParam;
import com.orchid.community.models.DTO.UserParamDTO;
import com.orchid.community.models.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/account")
@Slf4j
public class AccountController {

    @Autowired
    private AccountService accountService;

    /**
     * 分页查看账号列表
     * @param userParamDTO
     * @return
     */
    @PostMapping("/getAccountList")
    public MsgResponse getAccountList(@RequestBody UserParamDTO userParamDTO) {
        return accountService.getAccountList(userParamDTO);
    }

    /**
     * 新建账号
     * @param accountParam
     * @return
     */
    @PostMapping("/addAccount")
    public MsgResponse addAccount(@RequestBody AccountParam accountParam){
        if(StringUtils.isEmpty(accountParam.getUsername())
                || StringUtils.isEmpty(accountParam.getName())
                || StringUtils.isEmpty(accountParam.getPhone())
        ){
            return ResultUtils.fail(CodeEnum.PARAM_ERROR);
        }
        if(!ValidateNumber.validatePhone(accountParam.getPhone())){
            return ResultUtils.fail("手机号不正确");
        }
        return accountService.addAccount(accountParam);
    }


    /**
     * 编辑账号
     * @param accountParam
     * @return
     */
    @PostMapping("/editAccount")
    public MsgResponse editAccount(@RequestBody AccountParam accountParam){
        if(StringUtils.isEmpty(accountParam.getUsername())
                || StringUtils.isEmpty(accountParam.getName())
                || StringUtils.isEmpty(accountParam.getPhone())
                || StringUtils.isEmpty(accountParam.getId())
        ){
            return ResultUtils.fail(CodeEnum.PARAM_ERROR);
        }
        if(!ValidateNumber.validatePhone(accountParam.getPhone())){
            return ResultUtils.fail("手机号不正确");
        }
        return accountService.editAccount(accountParam);
    }


    /**
     * 重置密码
     * @param accountParam
     * @return
     */
    @PostMapping("/resetAccount")
    public MsgResponse resetAccount(@RequestBody AccountParam accountParam){
        if(StringUtils.isEmpty(accountParam.getId())){
            return ResultUtils.fail(CodeEnum.PARAM_ERROR);
        }
        return accountService.resetAccount(accountParam);
    }

    /**
     * 冻结|解冻账号
     * @param accountParam
     * @return
     */
    @PostMapping("/freezeAccount")
    public MsgResponse freezeAccount(@RequestBody AccountParam accountParam){
        Integer id = Integer.valueOf(accountParam.getId());
        String kind = accountParam.getKind();
        if(StringUtils.isEmpty(accountParam.getId()) || StringUtils.isEmpty(accountParam.getKind())){
            return ResultUtils.fail(CodeEnum.PARAM_ERROR);
        }
        return accountService.freezeAccount(accountParam);
    }

    @PostMapping("/delAccount")
    public MsgResponse delAccount(@RequestBody AccountParam accountParam){
        if(StringUtils.isEmpty(accountParam.getId())){
            return ResultUtils.fail(CodeEnum.PARAM_ERROR);
        }
        Integer id = Integer.valueOf(accountParam.getId());
        return accountService.delAccount(id);
    }



}
